package cn.dac.service.impl;

import cn.dac.service.SecurityService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class SecurityServiceImpl implements SecurityService {
   private Logger logger = LoggerFactory.getLogger(SecurityServiceImpl.class);


    @Override
    public void getOnlineUserList() {

    }

    @Override
    public void removeOldUserSession() {
        SessionsSecurityManager securityManager = (SessionsSecurityManager) SecurityUtils.getSecurityManager();
        DefaultSessionManager sessionManager = (DefaultSessionManager) securityManager.getSessionManager();
        Collection<Session> sessions = sessionManager.getSessionDAO().getActiveSessions();//获取当前已登录的用户session列表
        logger.debug(sessions.isEmpty()+"");
        for (Session session : sessions) {
            logger.debug(session.getId()+"");
            /*if (SecurityUtils.getSubject().getSession().getId().equals(session.getId())){
                System.out.println(username + "已登录，剔除中...");
                sessionManager.getSessionDAO().delete(session);
            }*/
        }
    }
}
